#
# @lc app=leetcode.cn id=819 lang=python3
#
# [819] 最常见的单词
#

# @lc code=start
class Solution:
    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
        n = len(paragraph)
        lower_str = paragraph.lower()
        word_map = {}
        L = 0
        while L < n:
            while L < n and not self.__isalpha(ord(lower_str[L])):
                L += 1
            R = L
            while R < n and self.__isalpha(ord(lower_str[R])):
                R += 1
            word = lower_str[L:R]
            word_map.update({word: word_map.get(word, 0) + 1})
            L = R

        sorted_wm = sorted(word_map.items(), key=lambda x: x[1], reverse=True)
        for k, v in sorted_wm:
            if k not in banned:
                return k
        return ""  # unreachable

    def __isalpha(self, c: int) -> bool:
        return c >= 97 and c <= 122

# @lc code=end
